/**
 * 演示页面 的 MOCK 数据
 */
 import mockjs from 'mockjs';

 const startKey = "767654545434"
 
 // 样本数据
 const sampleItem = {
   key: startKey,
   name: "客户姓名",
   rent: {val: 3, unit: "天"},
   birth: 1628416405,
   tags: ["密封", "干燥", "避光"],
   create_time: 1628072476,
   update_time: 1628072476,
   create_by: "小侠女"
 }
 
 // 构造20条测试数据
 const mockDataSize = 20
 const testData = []
 let newItem
 for (let i = 0; i < mockDataSize; i += 1) {
   newItem = JSON.parse(JSON.stringify(sampleItem)) //  深拷贝复制一条数据
   newItem.key = String(Number(newItem.key) + i) // 自增key模拟
   newItem.name += String(i) // 姓名模拟
   newItem.rent.val += i // rent模拟
   newItem.birth += i // birth模拟
   testData.push(newItem)
 }
 
 export default {
   'POST /api/pagedemo/list': (req, res) => {
     res.send(
       mockjs.mock(
         {
           ret: 0,
           data: {
             list: testData,
             total: testData.length,
             query: '',
           }
         }
       ),
     );
   },
 
   'POST /api/pagedemo/getData': (req, res) => {
     const { id } = req.body;
     let queryData = {}
     for(let i=0; i< testData.length; i += 1){
       if( testData[i].key == id )
         queryData = testData[i]
     }
     res.send(
       mockjs.mock(
         {
           ret: 0,
           data: {
             list: [queryData],
             total: 1,
             query: '',
           }
         }
       ),
     );
   },
 
 
   'POST /api/pagedemo/add': (req, res) => {
     const { data } = req.body;
     data.key = String(Number(startKey) + testData.length + 1)
     // 添加新数据
     testData.push(data)
     res.send(
       mockjs.mock(
         {
           ret: 0, // 通信成功
           data: {
             ret: 0, // 添加成功
           }
         }
       ),
     );
   },
 };
 